package br.com.mayco.controller;

import java.io.File;
import java.io.Serializable;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 * @author Mayco Lucian
 * @since 25/02/2013
 * @version 1.0.0
 */
public class BaseBean implements Serializable {

    private Messages messages = new Messages();

    public Messages getMessages() {
        return messages;
    }

    public BaseBean() {
    }

    public void put(String key, Object value) {
        FacesContext fctx = FacesContext.getCurrentInstance();
        ExternalContext ectx = fctx.getExternalContext();
        HttpSession session = (HttpSession) ectx.getSession(true);
        session.setAttribute(key, value);
    }

    public Object get(String key) {
        FacesContext fctx = FacesContext.getCurrentInstance();
        ExternalContext ectx = fctx.getExternalContext();
        HttpSession session = (HttpSession) ectx.getSession(true);
        Object attribute = session.getAttribute(key);
        return attribute;
    }

    public static String getRealPath() {
        FacesContext fctx = FacesContext.getCurrentInstance();
        ExternalContext ectx = fctx.getExternalContext();
        return ectx.getRealPath("");
    }

    public static String getVirtualPath() {
        FacesContext fctx = FacesContext.getCurrentInstance();
        ExternalContext ectx = fctx.getExternalContext();
        return String.format("%s:%d/%s", ectx.getRequestServerName(), ectx.getRequestServerPort(), ectx.getRequestContextPath());
    }

    public String getRealPath(String filename) {
        return getRealPath() + File.separator + filename;

    }
}
